package nc.ui.am.common.utils;

import nc.ui.bd.ref.AbstractRefGridTreeModel;
import nc.ui.bd.ref.AbstractRefModel;
import nc.ui.bd.ref.AbstractRefTreeModel;
import nc.ui.bd.ref.IRefConst;
import nc.ui.pub.beans.UIRefPane;
import nc.ui.pub.beans.textfield.UITextType;
import nc.ui.querytemplate.meta.FilterMeta;
import nc.ui.querytemplate.valueeditor.IFieldValueElementEditor;
import nc.ui.querytemplate.valueeditor.IFieldValueElementEditorFactory;
import nc.ui.querytemplate.valueeditor.RefElementEditor;

/**
 * 
 * @author yangjl
 * @version 2009-1-6
 */
public class RefOfDefElementEditorFactory implements IFieldValueElementEditorFactory {
	private final String refName;
	private final String pk_corp;
	private final String fieldCode;

	public RefOfDefElementEditorFactory(String refName, String pk_corp, String fieldCode) {
		this.refName = refName;
		this.pk_corp = pk_corp;
		this.fieldCode = fieldCode;
	}

	public IFieldValueElementEditor createFieldValueElementEditor(FilterMeta meta) {
		if (fieldCode.equals(meta.getFieldCode())) {
			UIRefPane refPane = ClientUtils.createRefPane(pk_corp, refName);
			if (refPane != null) {
				AbstractRefModel refModel = refPane.getRefModel();
				refModel.setSealedDataShow(true);
				if (refModel instanceof AbstractRefGridTreeModel) {
					refPane.setRefType(IRefConst.GRIDTREE);
				} else if (refModel instanceof AbstractRefTreeModel) {
					refPane.setRefType(IRefConst.TREE);
				}
				refPane.setRefModel(refModel);
				refPane.setTextType(UITextType.TextStr);
				refPane.setReturnCode(false);
			}
			return new RefElementEditor(refPane, meta.getReturnType());
		}
		return null;
	}
}